package com.itheima.service.impl;

import com.itheima.domain.Student;
import com.itheima.domain.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherServiceImpl implements TeacherService{

    @Override
    public void addTeacher(ArrayList<Teacher> list) {
        Scanner sc = new Scanner(System.in);
        Teacher st = new Teacher();
        System.out.println("请输入你要添加的老师的编号:");
        String sid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                System.out.println("你输入的老师编号已经存在了,请重新输入老师编号:");
                //sid = sc.nextLine();
                sid = sc.next();
                i = -1;
            }
        }
        st.setSid(sid);
        System.out.println("请输入你要添加的老师的姓名:");
        st.setName(sc.next());
        System.out.println("请输入你要添加的老师的年龄:");
        st.setAge(sc.next());
        System.out.println("请输入你要添加的老师的生日:");
        st.setBirthday(sc.next());
        System.out.println("请输入你要添加的老师的地址:");
        st.setAddress(sc.next());
        list.add(st);
        System.out.println("恭喜您,您成功添加了老师!");
    }

    @Override
    public void deleteTeacherById(ArrayList<Teacher> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要删除的id:");
        String id;
        while(true){
            id=sc.next();
            for (Teacher teacher:list) {
                if(teacher.getSid().equals(id)){
                    list.remove(teacher);
                    return ;
                }
            }
            System.out.println("你输入的教师编号没找到对应的教师!");
            return;
        }

    }

    @Override
    public void updateTeacher(ArrayList<Teacher> list) {
        int idl;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的教师编号:");
        Teacher st = new Teacher();
        String sid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSid().equals(sid)) {
                System.out.println("找到了你要修改的教师!");
                idl = i;
                System.out.println("请输入修改后的教师编号:");
                String xsid = sc.next();
                exit:while (true) {
                    if(list.get(i).getSid().equals(xsid)){
                        break exit;
                    }
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        if (list.get(i1).getSid().equals(xsid)) {
                            System.out.println("新教师编号有冲突,请您重新输入:");
                            xsid = sc.next();
                            i1=-1;
                        }
                }
                    st.setSid(xsid);
                    break exit;
                }
                System.out.println("请输入修改后教师的姓名:");
                String name = sc.next();
                st.setName(name);
                System.out.println("请输入修改后教师的年龄:");
                st.setAge(sc.next());
                System.out.println("请输入修改后教师的生日:");
                st.setBirthday(sc.next());
                System.out.println("请输入修改后教师的地址:");
                st.setAddress(sc.next());
                list.set(idl, st);
                return;
            }
        }
        System.out.println("不好意思,没有找到您要的教师编号相关的教师信息!!!");
    }

    @Override
    public void findAllTeacher(ArrayList<Teacher> list) {
        System.out.println("姓名\t\t教师编号\t\t年龄\t\t生日\t\t地址\t\t");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).show();
        }

    }
}
